<?php
//运行js文件  远程调用aip接口
function execujs($js_path, $func, $arg)
{
    $url = "http://runcode-api2-ng.dooccn.com/compile2";
//    $js_path = "./php_cmd5x.txt";
    //读取js文件
    $js_data = read_file($js_path);
    $js_data .= "console.log(" . $func . "('" . $arg . "')" . ");";
//        echo "console.log(". $func . "('" . $arg . "')" . ");";
    $js_data = urlencode(base64_encode($js_data));
//    echo "console.log(". $func . "('" . $arg . "')" . ");";
    // 设置请求头
    $headers = [
        'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
        'Referer:http://www.dooccn.com/',
        'Content-Type:application/x-www-form-urlencoded; charset=UTF-8'
    ];
    $data = array(
        "stdin" => "",
        "language" => "4"
    );
    $postdata = http_build_query($data) . "&code=" . $js_data;
//    echo $postdata;
    $res = curl_post($url, $postdata, $headers);
    $json = json_decode($res);
//    print_r( $res );
    return str_replace("\n", "", $json->output);
}

//读文件
function read_file($filename)
{
    $handle = fopen($filename, "r");//读取二进制文件时，需要将第二个参数设置成'rb'
    //通过filesize获得文件大小，将整个文件一下子读到一个字符串中
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    return $contents;
}

//curl post 请求
function curl_post($url, $data = array(), $headers = array())
{

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    // POST数据

    curl_setopt($ch, CURLOPT_POST, 1);
    // 设置请求头
//    $headers = [
//        'User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
//        'Referer : http://www.dooccn.com/',
//        'Content-Type : application/x-www-form-urlencoded; charset=UTF-8'
//    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // 把post的变量加上

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $output = curl_exec($ch);

    curl_close($ch);

    return $output;

}

//获取html
function curl_get($iqiyi_url, $headers = array())
{
    if (empty($iqiyi_url)) {
        return false;
    }

    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL, $iqiyi_url);//抓取指定网页

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//url跳转自动抓取
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//如果成功只将结果返回，不自动输出任何内容

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器

    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
}

//解析爱奇艺视频具体信息,返回json对象
function parse($iqiyi_url)
{
    $str = curl_get($iqiyi_url);
    $pattern = "/Q.PageInfo.playPageInfo \|\| (\{.+?\})/";
    preg_match_all($pattern, $str, $match);
    $json_str = $match[1][0];
//    echo "@".$json_str."@";
//    print_r($match[1][0]);
    return json_decode($json_str, true);
}

//向爱奇艺发起请求拿到数据
function getm3u8($iqiyi_url)
{
    $video_info = parse($iqiyi_url);
//    print_r($video_info);
    if ($video_info == null) {
        echo "无效的爱奇艺链接，请输入电脑版网站链接";
        return;
    }
    $url = "https://cache.video.iqiyi.com";
    $tm = time() * 1000;
//    echo $tm."<br>";
    //进行数据加密
    $authkey = "123456";
    $tvId = number_format($video_info['tvId'],0,'','');
    $authkey = execujs("./php_authkey.txt", "t.a", 'd41d8cd98f00b204e9800998ecf8427e' . $tm . $tvId);
    if ($authkey == "") {
        echo "内部错误1，请稍后再试";
        return;
    }
//    echo "@".$authkey."@";
    $data = "/dash?" .
        "tvid=" . $tvId
        . "&bid=600"
        . "&vid=" . $video_info['vid']
        . "&src=01010031010000000000"
        . "&vt=0"
        . "&rs=1"
        . "&uid="
        . "&ori=pcw"
        . "&ps=0"
        . "&k_uid="
        . "&pt=0"
        . "&d=0"
        . "&s="
        . "&lid="
        . "&cf="
        . "&ct="
        . "&authKey=" . $authkey
        . "&k_tag=1"
        . "&ost=0"
        . "&ppt=0"
        . "&dfp="
        . "&locale=zh_cn"
        . "&prio=%7B%22ff%22:%22f4v%22,%22code%22:2%7D"
        . "&pck=69IXL2M1BHFpk94Vm3ZqKp9GqTh3JAq1WnAoQpFDuipwDkLly6W2o0EUR0sQnwvu7uUe0"
        . "&k_err_retries=0"
        . "&up="
        . "&qd_v=2"
        . "&tm=" . $tm
        . "&qdy=i"
        . "&qds=0"
        . "&k_ft1=706436220846084"
        . "&k_ft4=35184440778756"
        . "&k_ft5=1&bop=%7B%22version%22:%2210.0%22,%22dfp%22:%22a%22%7D"
        . "&ut=1";
//        echo $data."<br>";
    $vf = "";

    $vf = execujs("./php_cmd5x.txt", "cmd5x_exports.cmd5x", $data);
    if ($vf == "") {
        echo "内部错误2，请稍后再试";
        return;
    }
//        echo "@".$vf."@";
    $data .= "&vf=" . $vf;
    $url .= $data;
//    echo $url;
    //设置请求头
    $headers = ['User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
        'Host:www.iqiyi.com',
        'Referer:www.iqiyi.com',
        'Content-Type:application/x-www-form-urlencoded; charset=UTF-8'];
    $res = curl_get($url, $headers);
//    print_r($res);
    $video_obj = json_decode($res);
    $videos = $video_obj->data->program->video;
    foreach ($videos as $video) {
        if ($video->_selected == true) {
            echo($video->m3u8);
        }
    }

}

//header('Content-Type:application/json; charset=utf-8');
//echo execujs("./php_authkey.txt","t.a","123456");
$url = $_GET['url'];
if ($url == "") {
    echo "url为空";
} else {
//    getm3u8('https://www.iqiyi.com/v_ufug3iz1w0.html');
    getm3u8($url);
}
//echo curl_get("https://cache.video.iqiyi.com/dash?tvid=2971723654288700&bid=600&vid=07fdccea5e85c8fcd4d5090cd5cc53fc&src=01010031010000000000&vt=0&rs=1&uid=&ori=pcw&ps=0&k_uid=&pt=0&d=0&s=&lid=&cf=&ct=&authKey=d954939a26dfc472190e72afe1b4a08c&k_tag=1&ost=0&ppt=0&dfp=&locale=zh_cn&prio=%7B%22ff%22:%22f4v%22,%22code%22:2%7D&pck=69IXL2M1BHFpk94Vm3ZqKp9GqTh3JAq1WnAoQpFDuipwDkLly6W2o0EUR0sQnwvu7uUe0&k_err_retries=0&up=&qd_v=2&tm=1620634054000&qdy=i&qds=0&k_ft1=706436220846084&k_ft4=35184440778756&k_ft5=1&bop=%7B%22version%22:%2210.0%22,%22dfp%22:%22a%22%7D&ut=1&vf=3b094ff3e5c43af046cee4d45b3efa21");
